import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'

const files = import.meta.glob('./modules/*.js', {
  eager: true,
})

const modules = []
Object.keys(files).forEach(key => {
  const moduleName = key.substring(key.lastIndexOf('/') + 1, key.lastIndexOf('.js'))
  modules[moduleName] = files[key].default
})

// 持久化存储
const vuexLocal = new VuexPersistence({
  storage: window.localStorage,
  modules: ['user', 'category'],
})

Vue.use(Vuex)

const store = new Vuex.Store({
  // 严格模式
  strict: true,
  modules,
  plugins: [vuexLocal.plugin],
})

export default store
